﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hengbei.DAL;
using System.Data;
using System.Configuration;
using Hengbei.DAL.Models; 

namespace Hengbei.BLL
{
    public static class UserService
    {
        private static HengbeiEntities context;

        public static HengbeiEntities Context
        {
            get 
            {
                if (context == null)
                {
                    context = new HengbeiEntities();
                }
                return context; 
            }
            set { context = value; }
        }

        public static IList<SiteUser> GetAllUsers()
        {
            List<SiteUser> users = Context.SiteUsers.ToList();
            return users;
        }

        public static int AddUser(SiteUser user)
        {
            Context.SiteUsers.Add(user);
            int result = Context.SaveChanges();
            return result;
        }

        public static int UpdateUser(SiteUser user)
        {
            var userFound = Context.Entry(user);
            if (userFound != null)
            {
                userFound.State = EntityState.Modified;
            }
            int result = Context.SaveChanges();
            return result;
        }

        public static int DeleteUser(SiteUser user)
        {
            var userFound = Context.Entry(user);
            if (userFound != null)
            {
                userFound.State = EntityState.Deleted;
            }
            int result = Context.SaveChanges();
            return result;
        }

        public static bool IfUserLoginIDExists(string loginID)
        {
            bool isUserLoginIDExisting = false;
            isUserLoginIDExisting = Context.SiteUsers.Where(u => u.LoginID == loginID).Count() > 0;
            return isUserLoginIDExisting;
        }

        public static bool ValidateUser(string userName, string password)
        {
            bool hasPassedValidation = false;
            hasPassedValidation = Context.SiteUsers.Where(u => u.LoginID == userName && u.Password == password).Count() > 0;
            return hasPassedValidation;
        }

        public static bool IsUserInRole(string userName, string roleName)
        {
            bool hasPassedValidation = false;
            hasPassedValidation = Context.UserInRoles.Include("SiteUser").Include("UserRole")
                .Where(u => u.SiteUser.LoginID == userName && u.UserRole.RoleID == roleName).Count() > 0;
            return hasPassedValidation;
        }

        public static List<string> GetAllRoles(string userName)
        {
            List<string> roles = new List<string>();

            long userID = Context.SiteUsers.Where(u => u.LoginID == userName).FirstOrDefault().ID;

            string[] userRoles = Context.UserInRoles.Include("UserRole")
                .Where(r => r.UserID == userID).Select(r => r.UserRole.RoleID).ToArray();

            roles.AddRange(userRoles);
            return roles;
        }
    }
}
